#include"progress.h"


const char symbol = '#';
char percentage[101];
double speed[] = {10.0, 5.0, 1.0, 0.05, 0.02};

void print_progress(const char* tips, double current, double total){
  double rate = current * 100.0 / total; 
  const char* cursor = "|/-\\";
  int i = 0, len = strlen(cursor);
  for(i = 0; i < (int)rate; i++) percentage[i] = symbol;
  static int ind = 0;
  ind /= len;
  printf("%s...[%-100s][%lf%%][%c]\r",tips, percentage, rate, cursor[ind]);
}

void download(double total, call cb){
  srand(time(0));
  double cur = 0.0;
  while(cur <= total){
    cb("下载中", cur, total);
    if(cur >= total) break;
    fflush(stdout);
    usleep(5000); 
    int rand_ind = rand() % 6;
    cur += speed[rand_ind];
    if(cur > total) cur = total;
  }
  printf("\n");
  printf("下载完成：本次一共下载%lf MB\n",total);
}
